<template>
	<xn-form-container
		:title="formData.id ? '编辑流程委托' : '增加流程委托'"
		:width="500"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="受托人：" name="agentUser">
				<xn-user-selector
					:org-tree-api="selectorApiFunction.orgTreeApi"
					:user-page-api="selectorApiFunction.userPageApi"
					:radio-model="true"
					v-model:value="formData.agentUser"
				/>
			</a-form-item>
			<a-form-item label="委托流程：" name="process">
				<xn-model-selector :model-page-api="selectorApiFunction.modelPageApi" v-model:value="formData.process" />
			</a-form-item>
			<a-form-item label="委托时间：" name="startEndTime">
				<a-range-picker
					v-model:value="formData.startEndTime"
					show-time
					:placeholder="['开始时间', '结束时间']"
					value-format="YYYY-MM-DD HH:mm:ss"
					style="width: 100%"
				/>
			</a-form-item>
			<a-form-item label="原因：" name="cause">
				<a-textarea
					v-model:value="formData.cause"
					placeholder="请输入原因"
					:auto-size="{ minRows: 2, maxRows: 5 }"
					allow-clear
				/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="flwAgentForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import flwAgentApi from '@/api/flw/flwAgentApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			recordData.process = JSON.parse(recordData.process)
			formData.value = Object.assign({}, recordData)
			// 转换时间
			if (formData.value.startTime && formData.value.endTime) {
				formData.value.startEndTime = [formData.value.startTime, formData.value.endTime]
				delete formData.value.startTime
				delete formData.value.endTime
			}
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		category: [required('请选择委托分类')],
		agentUser: [required('请选择受托人')],
		process: [required('请选择委托流程')],
		startEndTime: [required('请选择时间范围')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				formDataParam.process = JSON.stringify(formDataParam.process)
				// 转换时间
				if (formDataParam.startEndTime) {
					formDataParam.startTime = formDataParam.startEndTime[0]
					formDataParam.endTime = formDataParam.startEndTime[1]
					delete formDataParam.startEndTime
				}
				flwAgentApi
					.flwAgentSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 传递选择器需要的API
	const selectorApiFunction = {
		orgTreeApi: (param) => {
			return flwAgentApi.flwAgentTree(param).then((orgTree) => {
				return Promise.resolve(orgTree)
			})
		},
		userPageApi: (param) => {
			return flwAgentApi.flwAgentUserSelector(param).then((data) => {
				return Promise.resolve(data)
			})
		},
		modelPageApi: (param) => {
			return flwAgentApi.flwAgentUserModelSelector(param).then((data) => {
				return Promise.resolve(data)
			})
		}
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
